package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.common.result.Result;
import cn.wolfcode.luowowo.common.result.StatusCode;
import cn.wolfcode.luowowo.member.domain.Collects;
import cn.wolfcode.luowowo.member.domain.SiteCollect;
import cn.wolfcode.luowowo.member.domain.TravelAgency;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.CollectsMapper;
import cn.wolfcode.luowowo.member.service.ICollectsService;
import cn.wolfcode.luowowo.member.service.ISiteCollectService;
import cn.wolfcode.luowowo.member.service.ITravelAgencyService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.CollectsVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CollectsServiceImpl extends StatusCode implements ICollectsService{
    /**
     * 项目名称：luowowo
     * 类描述： 我的收藏类的功能
     * 类名称：cn.wolfcode.luowowo.member.service.impl;
     * 创建人：luozijian
     * 创建时间：2020-04-05 00:53
     * @version XXX
     */
    public static final int COLLECT_SITE=1;//目的收藏
    public static final int COLLECT_TRAVEL = 2;//游记收藏
    public static final int COLLECT_STRATEGY= 3;//攻略收藏
    public static final int COLLECT_AGENCY = 4;//旅行社收藏

    @Autowired
    private CollectsMapper collectsMapper; //收藏的服务
    @Reference
    private ITravelService travelService; //游记服务
    @Reference
    private IUserInfoService userInfoService; //用户服务
    @Reference
    private IStrategyDetailService strategyDetailService;//攻略服务
    @Reference
    private ITravelAgencyService travelAgencyService;//旅行社收藏服务
    @Reference
    private ISiteCollectService siteCollectService;//地点收藏服务
    @Reference
    private IDestinationService destinationService;//目的地服务
    //当传进来的collect.getType()为n,就做n事情
    public void packagingParam(CollectsVo collect){
        switch (collect.getType()){
            /*case COLLECT_SITE:
                //地点收藏
                handlerDestinationParam(collect);
                break;*/
            case COLLECT_TRAVEL:
                //游记收藏
                handlerTravelParam(collect);
                break;
            case COLLECT_STRATEGY:
                //攻略收藏
                handlerStrategyParam(collect);
                break;
            case COLLECT_AGENCY:
                //旅行社收藏
                handlerAgencyParam(collect);
                break;
        }
    }
    /*//地点收藏
    public void handlerDestinationParam(CollectsVo collect) {
        SiteCollect site = siteCollectService.get(Long.valueOf(collect.getItemId()));
        siteCollectService.getStayNum();
        if (site!=null){
            collect.setFavtip(site.getFavtip());
            collect.setStayUrl(site.getStayUrl());
            collect.setStayName(site.getStayName());
            collect.setStayPlynum(site.getStayPlynum());
            collect.setNnt(site.getNnt());
            collect.setStayNum(site.getS);
        }
    }*/
    //游记收藏
    public void handlerTravelParam(CollectsVo collect){
        Travel travel = travelService.get(Long.valueOf(collect.getItemId()));
        if(travel != null){
            collect.setTitle(travel.getTitle());
            UserInfo userInfo = userInfoService.get(Long.valueOf(travel.getAuthor().getId()));
            if(userInfo != null){
                collect.setAuthorName(userInfo.getNickname());
                collect.setUserCover(userInfo.getHeadImgUrl());
            }
            collect.setCommentContent(travel.getSummary());
            collect.setCommentNum(travel.getReplynum());
            collect.setViewNum(travel.getViewnum());
            collect.setCoverUrl(travel.getCoverUrl());
            collect.setId(travel.getId());
        }
    }

    //攻略收藏
    public void handlerStrategyParam(CollectsVo collect) {
        StrategyDetail detail = strategyDetailService.get(Long.valueOf(collect.getItemId()));
        if (detail != null){
            collect.setTitle(detail.getTitle());
            UserInfo userInfo = userInfoService.get(Long.valueOf(detail.getAuthor().getId()));
            if (userInfo!=null){
                collect.setAuthorName(userInfo.getNickname());
                collect.setUserCover(userInfo.getHeadImgUrl());
            }
            collect.setCommentContent(detail.getSummary());
            collect.setViewNum(detail.getViewnum());
            collect.setCommentNum(detail.getReplynum());
            collect.setCoverUrl(detail.getCoverUrl());
            collect.setId(detail.getId());
        }
    }
    //旅行社收藏
    public void handlerAgencyParam(CollectsVo collect) {
        TravelAgency travelAgency = travelAgencyService.get(Long.valueOf(collect.getItemId()));
        if(travelAgency!=null){
            collect.setName(travelAgency.getName());
            collect.setLocation(travelAgency.getLocation());
            collect.setPictureUrl(travelAgency.getPictureUrl());
            collect.setState(travelAgency.getState());
            collect.setId(travelAgency.getId());
        }
    }

    @Override
    public Result selectCollectsByParam(JSONObject jsonData) {
        //返回数据
        Result result = new Result();
        JSONObject returnData = new JSONObject();
        //类型 1: 地点  2:游记 3:攻略 4:旅行社
        Integer type = jsonData.getInteger("type");
        //用户iD
        Integer userId = jsonData.getInteger("userId");

        /**
         * 根据类型获取收藏列表
         */
        List<CollectsVo> collects = collectsMapper.selectByParam(jsonData);

        if(collects.size() > 0){
            for (CollectsVo collect : collects) {
                //packagingParam(collect);
            }
        }
        returnData.put("list",collects);
        result.setResultCode(RESULT_SUCCESS);
        result.setResultData(returnData);
        return result;
    }




}
